package com.epfresh.api.utils;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;

import com.epfresh.supplier.R;

import java.util.HashMap;

/**
 * Created by wzh on 16-11-4.
 */

public class SoundPlayUtils {
    // SoundPool对象
    public static SoundPool mSoundPlayer = new SoundPool(1,
            AudioManager.STREAM_SYSTEM, 5);
    public static SoundPlayUtils soundPlayUtils;
    // 上下文
    static Context mContext;
    //定义一个Map用来存放声音
    static HashMap<String, Integer> soundMap = new HashMap<String, Integer>();

    /**
     * 初始化
     *
     * @param context
     */
    public static SoundPlayUtils init(Context context) {
        if (soundPlayUtils == null) {
            soundPlayUtils = new SoundPlayUtils();
        }

        // 初始化声音
        mContext = context;
        soundMap.put("receiveOffer", mSoundPlayer.load(mContext, R.raw.offer, 1));

        soundMap.put("receiveOrder", mSoundPlayer.load(mContext, R.raw.order, 1));
        soundMap.put("overTime", mSoundPlayer.load(mContext, R.raw.order_timeout, 1));
        soundMap.put("abolishOrder", mSoundPlayer.load(mContext, R.raw.cancel_order, 1));
        soundMap.put("payOff", mSoundPlayer.load(mContext, R.raw.payment_deal, 1));
        soundMap.put("reject", mSoundPlayer.load(mContext, R.raw.rejection, 1));
        soundMap.put("rejectResult", mSoundPlayer.load(mContext, R.raw.rejection_result, 1));

        return soundPlayUtils;
    }

    /**
     * 播放声音
     *
     * @param soundID
     */
    public static void play(int soundID) {
        mSoundPlayer.play(soundID, 1, 1, 0, 0, 1);
    }


    public static void playSound(String key) {
        //拒收
        if (key != null && !"".equals(key)) {
            Log.e("sound", key);
            play(soundMap.get(key));
        }
    }
}
